/*
 * @lc app=leetcode.cn id=96 lang=cpp
 *
 * [96] 不同的二叉搜索树
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int numTrees(int n)
    {
        vector<vector<int>> dp(n + 10, vector<int>(n + 10, 0));
        for (int i = 0; i <= n; i++)
        {
            dp[i][i] = 1;
            dp[i][i + 1] = 2;
        }

        for (int step = 2; step < n; step++)
        {
            for (int i = 1; i <= n - step; i++)
            {
                int j = i + step;
                for (int k = i + 1; k < j; k++)
                {
                    dp[i][j] += dp[i][k - 1] * dp[k + 1][j];
                }
                dp[i][j] += dp[i + 1][j] + dp[i][j - 1];
            }
        }
        int ans = dp[1][n];
        return ans;
    }
};

// int main()
// {
//     int ans = Solution().numTrees(3);
//     cout << ans << endl;
//     return 0;
// }

// @lc code=end
